package com.d5xx.hunters.scoreboard;

import com.d5xx.hunters.API.Hunter;
import com.d5xx.hunters.API.HunterGame;
import com.d5xx.hunters.API.HunterPlayer;
import com.d5xx.hunters.Functions;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;


public class GameScoreBoard extends BukkitRunnable {
    private final Scoreboard scoreboard;
    private final Objective objective;
    private final String title;
    private final String[] prefix;
    private int i = 0;
    private final HunterPlayer player;
    private final String playertype;
    private final HunterGame game;

    public GameScoreBoard(HunterPlayer player) {
        String name = player.getPlayer().getUniqueId() + "-lobby";
        Functions.cleanScoreboard(name);
        this.player = player;
        this.game = Hunter.getGame(player.getPlayer().getWorld());
        this.playertype = game.getPlayerMode(player);
        this.title = "HUNTER GAME";
        this.prefix = new String[]{"§f§l", "§e§l", "§6§l"};
        this.scoreboard = Objects.requireNonNull(Bukkit.getScoreboardManager()).getMainScoreboard();
        this.objective = scoreboard.registerNewObjective(name, Criteria.DUMMY,"Lobby");
        this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    }
    @Override
    public void run() {
        if (i > title.length() + 10) {
            i = 0;
        }else {
            if (i < title.length()) {
                char Char1 = title.charAt(i);
                String color1 = title.substring(0,i);
                String color2 = title.substring(i + 1);
                color1 = prefix[0] + color1;
                color2 = prefix[1] + color2;
                objective.setDisplayName(color1 + prefix[2] + Char1 + color2);
            }else if (i == title.length() + 1) {
                objective.setDisplayName(prefix[0] + title);
            }else if (i == title.length() + 2) {
                objective.setDisplayName(prefix[1] + title);
            }else if (i == title.length() + 3) {
                objective.setDisplayName(prefix[0] + title);
            }else if (i >= title.length() + 4) {
                objective.setDisplayName(prefix[1] + title);
            }
            i++;
        }
        Date currentDate = new Date(); // 获取当前日期

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式化日期为字符串
        String date = sdf.format(currentDate);

        objective.getScore(ChatColor.GRAY+ date).setScore(5);
        objective.getScore(" ").setScore(4);
        objective.getScore(player.getKeyMessage("game-scoreboard-player-type").replace("%type%", playertype)).setScore(3);
        objective.getScore(player.getKeyMessage("game-scoreboard-game-type").replace("%type%", game.getType())).setScore(2);
        objective.getScore("").setScore(1);
        objective.getScore(player.getKeyMessage("server-ip")).setScore(0);
        player.getPlayer().setScoreboard(scoreboard); //为玩家设置计分板
    }
}
